<template>
  <a-modal :maskClosable="false"
    :title="title"
    :width="1024"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" v-bind="layout" :model="model" :rules="validatorRules">
        <a-form-model-item label="店内营业时间" >
          <a-time-picker use12-hours format="hh:mm"  valueFormat="hh:mm" v-model="model.storeTimeBegin" />
          ~
          <a-time-picker use12-hours format="hh:mm"  valueFormat="hh:mm" v-model="model.storeTimeEnd"/>
        </a-form-model-item>
        <a-form-model-item label="纬度" required prop="positionLat">
          <a-input placeholder="请输入门店的纬度" v-model="model.positionLat" />
        </a-form-model-item>
        <a-form-model-item label="经度" required prop="positionLng">
          <a-input placeholder="请输入门店的经度" v-model="model.positionLng" />
        </a-form-model-item>
        <a-form-model-item label="详细地址" required prop="address">
          <a-input placeholder="请输入门店的详细地址" v-model="model.address" />
        </a-form-model-item>
      </a-form-model>
      <div id="mycontainer" style="width: 100%; height: 350px"></div>
    </a-spin>

  </a-modal>
</template>

<script>
import { httpAction, putAction, getAction, postAction } from '@/api/manage'

export default {
  name: 'ordStoreTableAreaModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      layout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 14 }
      },
      confirmLoading: false,
      validatorRules: {
        positionLat: [{ required: true, message: '请输入门店的纬度!' }],
        positionLng: [{ required: true, message: '请输入门店的经度!' }],
        address: [{ required: true, message: '请输入门店的详细地址!' }],
        storeTimeBegin: [{ required: true, message: '请选择店内营业时间!' }],
        storeTimeEnd: [{ required: true, message: '请选择店内营业时间!' }],
      },
      url: {
        add: '/store/info',
        getSetup: '/store/info/setup',
      },
      map:null,
    }
  },
  created() {
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.initMap();

  },
  methods: {
    initMap() {
      let center =  new TMap.LatLng(39.984120,116.307484)
      //初始化地图
      let map = new TMap.Map('mycontainer', {
        zoom: 15,//设置地图缩放级别
        center: center//设置地图中心点坐标
      })
      this.map = map
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      const that=this;
      that.model.id = record.id;
      getAction(this.url.getSetup+"/"+record.id).then((res) => {
        if (res.success) {
          that.model=res.result;
        }
      });
      that.visible = true
    },
    close() {
      this.$refs.form.clearValidate()
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          postAction(that.url.add, this.model).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
              that.close()
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    }

  }
}
</script>

<style scoped>

</style>
